package com.dengpp.pdform.holder;

import android.app.DatePickerDialog;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import com.dengpp.pdform.config.AttributeConfig;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.widget.EditText; // 改为普通的 EditText
public class DateViewHolder extends BaseAttributeViewHolder {
    private EditText editText;
    private TextView tvValue;
    private TextView tvTitle;
    private AttributeConfig currentAttribute;

    public DateViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tv_title);
        editText = itemView.findViewById(R.id.edit_text);
        tvValue = itemView.findViewById(R.id.tv_value);
    }

    @Override
    public void bind(AttributeConfig attribute, boolean isEditMode, AttributeAdapter.OnAttributeChangeListener listener) {
        // 保存当前绑定的属性
        this.currentAttribute = attribute;

        // 移除旧的点击监听器
        editText.setOnClickListener(null);

        // 使用带星号的标题
        tvTitle.setText(getTitleWithRequiredStar(attribute.getTitle(), attribute.isRequired()));

        if (isEditMode && attribute.isEditable()) {
            editText.setVisibility(View.VISIBLE);
            tvValue.setVisibility(View.GONE);

            String currentValue = attribute.getValue() != null ? attribute.getValue().toString() : "";
            editText.setText(currentValue);

            // 设置新的点击监听器
            editText.setOnClickListener(v -> {
                // 确保当前属性仍然匹配
                if (currentAttribute == attribute) {
                    showDatePicker(attribute, listener);
                }
            });
            editText.setFocusable(false);

        } else {
            editText.setVisibility(View.GONE);
            tvValue.setVisibility(View.VISIBLE);
            tvValue.setText(attribute.getValue() != null ? attribute.getValue().toString() : "");
        }
    }

    /**
     * 在 ViewHolder 被回收时清理资源
     */
    @Override
    public void onViewRecycled() {
        super.onViewRecycled();
        editText.setOnClickListener(null);
        currentAttribute = null;
    }

    private void showDatePicker(AttributeConfig attribute, AttributeAdapter.OnAttributeChangeListener listener) {
        Calendar calendar = Calendar.getInstance();

        // 如果有当前值，解析为日历
        if (attribute.getValue() != null) {
            try {
                String dateFormat = attribute.getDateFormat() != null ?
                        attribute.getDateFormat() : "yyyy-MM-dd";
                SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
                calendar.setTime(sdf.parse(attribute.getValue().toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        DatePickerDialog datePicker = new DatePickerDialog(
                itemView.getContext(),
                (view, year, month, dayOfMonth) -> {
                    String dateFormat = attribute.getDateFormat() != null ?
                            attribute.getDateFormat() : "yyyy-MM-dd";
                    String date = String.format(Locale.getDefault(), dateFormat
                            .replace("yyyy", "%04d")
                            .replace("MM", "%02d")
                            .replace("dd", "%02d"), year, month + 1, dayOfMonth);

                    editText.setText(date);
                    if (listener != null) {
                        listener.onAttributeChanged(attribute.getKey(), date);
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );
        datePicker.show();
    }
}